package com.wu.llpsw.item.gongFa;

import com.wu.llpsw.yuanli.energy.yuanliManager;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.function.Predicate;

//元踆诀
public class yuan_zun_jue extends BowItem {
    float xiShu=0.1f;
    int timeLong=1000;
    public yuan_zun_jue(Properties p_41383_,float xi) {
        super(p_41383_);
        this.xiShu=xi;
    }

    @Override
    public InteractionResultHolder<ItemStack> use(Level p_40672_, Player p_40673_, InteractionHand p_40674_) {
        ItemStack itemstack = p_40673_.getItemInHand(p_40674_);
        p_40673_.startUsingItem(p_40674_);
        return InteractionResultHolder.pass(itemstack);
    }


    //添加描述
    @Override
    public void appendHoverText(ItemStack p_41421_, @Nullable Level p_41422_, List<Component> p_41423_, TooltipFlag p_41424_) {
        p_41423_.add(new TranslatableComponent("tooltip.wu.yuan_zun_jue.tooltip"));
    }


    @Override
    public int getUseDuration(ItemStack p_40680_) {
        return timeLong;
    }


    @Override
    public Predicate<ItemStack> getAllSupportedProjectiles() {
        return ARROW_ONLY;
    }

    //使用弓的动画
    @Override
    public UseAnim getUseAnimation(ItemStack p_40678_) {
        return UseAnim.BOW;
    }

    //获取使用时间，蓄力回复元力
    @Override
    public void releaseUsing(ItemStack p_41412_, Level world, LivingEntity entity, int time) {
        if (!world.isClientSide){
            if(entity instanceof Player){
                int out=(int) ((timeLong-time)*xiShu);
                Player player=(Player) entity;
                yuanliManager.use(player,out);
                //生成粒子效果
                ServerLevel serverWorld = (ServerLevel) player.getCommandSenderWorld();
                double x1 = player.getX();
                double y1 = player.getY();
                double z1 = player.getZ();
                for (int i = 0; i < 10; i++) {
                    serverWorld.sendParticles(ParticleTypes.ENCHANT, x1, y1+2, z1, 7, 0, 0, 0, 2);
                }
                world.playSound(player,player.getX(), player.getY(), player.getZ(), SoundEvents.CHORUS_FLOWER_GROW, SoundSource.PLAYERS, 1.0F, 1.0f);
                player.getCooldowns().addCooldown(this,20*4);
            }
        }
    }
}
